<%@page contentType="application/json;charset=UTF-8" %><%@page import="be.destin.skos.core.SkosManager" %><%@page import="be.destin.skos.html.SkosHtmlManager" %><%@page import="be.destin.skos.core.SchemeInterface" %><%@page import="be.destin.memolog.UsageMode" %><%@page import="be.destin.memolog.UsageLogger" %><%@page import="be.destin.memo.Log" %><%@page import="be.destin.memo.TagDef" %><%@page import="be.destin.memo.Tagged" %><%@page import="be.destin.records.UsageManager" %><%@page import="java.util.List" %><%@page import="java.net.URLDecoder" %><%
   SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
   SkosManager skosManager = skosHtmlManager.getSkosManager();
   String applic = request.getParameter("application");
   String user = request.getRemoteUser();
   String tag = request.getParameter("tag");
   String remove = request.getParameter("remove");
   boolean toRemove = false;
   if (remove != null && !remove.isEmpty()) {
	   remove = remove.toLowerCase();
	   if (remove.startsWith("on") || remove.startsWith("true")) {
		   toRemove = true;
	   }
   }
   String title = request.getParameter("title");

   UsageLogger tagLogger = new UsageLogger(request,"scheme",tag,UsageMode.TAGDEF);
   Log currentLogRec = tagLogger.stop();
   long internalId = tagLogger.getInternalId();
%>{"user":"<%=tagLogger.getUser() %>", <%
   if (internalId > 0) {
	   String rec = request.getParameter("record");
	   String url = request.getParameter("url");
	   UsageLogger usageLogger = new UsageLogger(request,applic,rec,UsageMode.TAGGED);
	   if (url != null && !url.isEmpty()) {
			UsageManager.fillUsageLogger(skosManager, usageLogger, URLDecoder.decode(url));
			applic = usageLogger.getApplication();
			rec = usageLogger.getRecord();
	   }
	   %>"application":"<%=applic%>","record":"<%=rec%>","tag":"<%=tag %>", <%
	   usageLogger.setTagHead((TagDef)currentLogRec);
	   Log[] currentTags = usageLogger.getLoggerRec();
	   Tagged currentTag = null;
	   for (Log oneEntry : currentTags) {
		   if (oneEntry instanceof Tagged) {
			   TagDef tagDefinition = ((Tagged)oneEntry).getTaghead();
			   if (tagDefinition != null && internalId == tagDefinition.getId()) {
				   currentTag = (Tagged)oneEntry;
				   break;
			   }
		   }
	   }
	   if (toRemove && currentTag != null) {
		   usageLogger.getContext().deleteObject(currentTag);
		   %>"action":"removed", "status":"notset", <%
	   } else if (!toRemove && currentTag == null) {
		   if (title != null && !title.isEmpty()) {
		   		usageLogger.setCachedTitle(title);
		   }
		   if (usageLogger.getContext() != null) usageLogger.create();
		   %>"action":"added","status":"set", <%
	   } else {
		   %>"action":"unchanged", <%
		   if (currentTag != null) {
			   %>"status":"set", <%
		   } else {
			   %>"status":"notset", <%
		   }
	   }
	   List<Tagged> allUserTagged = ((TagDef)currentLogRec).getTagged();
	   int size = 0;
	   if (allUserTagged != null) size = allUserTagged.size();
	   %>"size":<%=size%>, "time":"<%=usageLogger.getNow() %>"<%
   } else {
	   %>"error":"this tagging is not possible"<%
   }
%>}